//
//  BCNRegisterFormViewController.m
//  BCN Emergencies
//
//  Created by Ana Martínez on 12/15/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "BCNRegisterFormViewController.h"
#import "BCNHomeViewController.h"
#import "BCNPrefs.h"

@implementation BCNRegisterFormViewController

@synthesize scrollView;
@synthesize name;
@synthesize lastname;
@synthesize phone;
@synthesize identifierType;
@synthesize identifierNumber;

BOOL viewMoved = FALSE;
BOOL keyboardShown = FALSE;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.scrollView.contentSize = CGSizeMake(320, 460);
    
    phone.text = [BCNPrefs getInstance].number;
    identifierType.text = [BCNPrefs getInstance].identifierType;
    identifierNumber.text = [BCNPrefs getInstance].identifierNumber;
    name.text = [BCNPrefs getInstance].name;
    lastname.text = [BCNPrefs getInstance].lastname;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasHidden:)
                                                 name:UIKeyboardDidHideNotification
                                               object:nil];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)languageSwith:(id)sender{
    UIButton* button = (UIButton*) sender;
    [button setBackgroundImage:[UIImage imageNamed:@"check_ok.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"check_ok.png"] forState:UIControlStateSelected];

    [button setBackgroundImage:[UIImage imageNamed:@"check_ok.png"] forState:UIControlStateReserved];

}

- (IBAction)registrationCompleted:(id)sender{
    if (phone.text != nil)
        [BCNPrefs getInstance].number = phone.text;
    if (identifierType.text != nil)
        [BCNPrefs getInstance].identifierType = identifierType.text;
    if (identifierNumber.text != nil)
        [BCNPrefs getInstance].identifierNumber = identifierNumber.text;
    if (name.text != nil)
        [BCNPrefs getInstance].name = name.text;
    if (lastname.text != nil)
        [BCNPrefs getInstance].lastname = lastname.text;
    [[BCNPrefs getInstance] savePrefs];
    if ([[BCNPrefs getInstance] isFirstLaunch]) {
        [BCNPrefs getInstance].isFirstLaunch = NO;
        BCNHomeViewController *homeViewController = [[BCNHomeViewController alloc] initWithNibName:@"BCNHomeViewController" bundle:[NSBundle mainBundle]];
        [self.navigationController pushViewController:homeViewController animated:YES];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardShown )
        return;
    
    
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
        
        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height;
        frame.size.height += keyboardSize.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];
        
        viewMoved = YES;
    
    
    keyboardShown = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
        
        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height;
        frame.size.height -= keyboardSize.height;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];
        
        viewMoved = NO;
    }
    
    keyboardShown = NO;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}

- (BOOL) textViewShouldEndEditing:(UITextView *)textView {
    
    [textView resignFirstResponder];
    return NO;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

        [textField resignFirstResponder];
        return NO;
}



@end
